<template>
  <div>
    <el-table
      :data="store.getters['menu/getMenu']"
      border
      style="width: 100%"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="菜单编号" />
      <el-table-column prop="title" label="菜单名称" />
      <el-table-column prop="pid" label="上级菜单" />
      <el-table-column prop="icon" label="菜单图标" />
      <el-table-column prop="url" label="菜单地址" />
      <el-table-column label="状态">
        <!-- 
          slot-scope='item' 作用域槽口
          v-slot  作用域槽口
         -->
        <template v-slot="item">
          <div>
            {{ item.row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="item">
          <div>
            <el-button @click="edit(item.row.id)" type="primary" size="small"
              >编辑</el-button
            >
            <el-button @click="del(item.row.id)" type="danger" size="small"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { delMenu } from "../../request/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { defineEmits } from "vue";
//引入仓库的方法
import { useStore } from "vuex";
const store = useStore();
//页面一加载要触发行动
store.dispatch("menu/getMenuAction");
// console.log(store,'仓库')
const emit = defineEmits(["edit"]);
//封装一个编辑事件
const edit = (id:number) => {
  emit("edit",id);
};
//封装一个删查事件
const del = (id: number) => {
  ElMessageBox.confirm("你确定要删除这条数据吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      //调用删除接口
      delMenu({ id }).then((res: any) => {
        if (res.code == 200) {
          ElMessage.success(res.msg);
          store.dispatch("menu/getMenuAction");
        } else {
          ElMessage.error(res.msg);
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消删除",
      });
    });
};
</script>

<style scoped></style>
